package com.auto.survey.widget.wheel;

import android.app.Activity;
import android.app.Dialog;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.TextView;


import com.auto.survey.R;

import java.util.List;

/**
 * Author: liyb
 * Class:
 */

public class ChartsDialog extends Dialog {

    private Activity mContext;
    private String bankCardNameVale;
    private int postion;

    private TextView tvTitleView;

    public ChartsDialog(final Activity context, List<String> list, int selection) {
        super(context);
        mContext = context;
        getWindow().requestFeature(Window.FEATURE_NO_TITLE);
        getWindow().setGravity(Gravity.BOTTOM);
        getWindow().setBackgroundDrawable(
                new ColorDrawable(Color.parseColor("#00000000")));
        getWindow().setWindowAnimations(R.style.WheelDialogAnimation);
        View rootView = getLayoutInflater().inflate(
                R.layout.wheel_view, null);
        TextView cancel = (TextView) rootView.findViewById(R.id.cancel);
        final TextView done = (TextView) rootView.findViewById(R.id.done);
        done.setOnClickListener(v -> {
            if (bankCardName != null) {
                bankCardName.chartsData(postion, bankCardNameVale);
            }
            dismiss();
        });
        cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });

        int screenWidth = mContext.getWindowManager().getDefaultDisplay()
                .getWidth();
        ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(screenWidth,
                ViewGroup.LayoutParams.MATCH_PARENT);
        super.setContentView(rootView, params);
        WheelViewDialog wv = rootView.findViewById(R.id.wheel_view_wv);
        tvTitleView = rootView.findViewById(R.id.tv_whell_title);
        wv.setOffset(2);
        wv.setItems(list);
        wv.setSeletion(selection);
        bankCardNameVale = list.get(selection);
        postion = 0;
        wv.setOnWheelViewListener(new WheelViewDialog.OnWheelViewListener() {
            @Override
            public void onSelected(int selectedIndex, final String item) {
                bankCardNameVale = item;
                postion = selectedIndex-2;
            }
        });
    }

    public Dialog setTitle(String title){
        tvTitleView.setText(title);
        return this;
    }

    private ChartsData bankCardName;

    public interface ChartsData {
        void chartsData(int postion, String chartsData);
    }

    public void setDiaChartsData(ChartsData chartsData) {
        this.bankCardName = chartsData;
    }
}
